package de.uni_mannheim.informatik.ai.dame_ai.dame.domain;

import java.util.Arrays;

public class Board {
	public static final int BOARDSQUARES = 64;
	public static final int BLACKHOME = 55;
	public static final int WHITEHOME = 8;
	private Square[] board;
	private Piece[] piecesStatus;
	public Board(){
		this.board = new Square[BOARDSQUARES];
		this.piecesStatus = new Piece[BOARDSQUARES];
		initBoard();
		initPieceStatus();
	}
	public Board(Square[] squares, Piece[] pieces){
		this.board = squares;
		this.piecesStatus = pieces;
	}
	public static Board copyBoard(Board board){
		Square[] newBoard = new Square[board.getBoard().length];
		for(int i = 0; i< newBoard.length;i++){
			newBoard[i]= new Square(board.getBoard()[i]);
		}
		assert(Arrays.equals(newBoard,board.getBoard()));
		Piece[] newPieces = new Piece[board.getPiecesStatus().length];
		for(int i = 0; i< newBoard.length;i++){
			newPieces[i]= new Piece(board.getPiecesStatus()[i]);
		}
		assert(Arrays.equals(newPieces,board.getPiecesStatus()));
		return new Board(newBoard, newPieces);
	}
	private void initBoard(){
		for(int i=0;i<BOARDSQUARES;i++){
			board[i]=new Square();
		}
	}
	private void initPieceStatus(){
		for(int i = 0; i<8;i++){
			for(int j=0;j<8;j++){
				if(i<3){
					if(i%2==0){
						if(j%2==0){
							piecesStatus[i*8+j]=new Piece(Piece.EMPTY);
						} else piecesStatus[i*8+j]=new Piece(Piece.WHITE);
					} else {
						if(j%2==1){
							piecesStatus[i*8+j]=new Piece(Piece.EMPTY);
						} else piecesStatus[i*8+j]=new Piece(Piece.WHITE);
					}
				} else if(i>4){
					if(i%2==1){
						if(j%2==0){
							piecesStatus[i*8+j]=new Piece(Piece.BLACK);
						} else piecesStatus[i*8+j]=new Piece(Piece.EMPTY);
					} else {
						if(j%2==1){
							piecesStatus[i*8+j]=new Piece(Piece.BLACK);
						} else piecesStatus[i*8+j]=new Piece(Piece.EMPTY);
					}
				} else{
					piecesStatus[i*8+j]=new Piece(Piece.EMPTY);
				}
			}
		}
	}
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Board){
			Board temp = (Board)obj;
			if(Arrays.equals(board,temp.getBoard()) && Arrays.equals(piecesStatus,temp.getPiecesStatus())){
				return true;
			} else return false;
		} else return false;
	}
	public Board(Square[] board){
		this.board = board;
	}
	public Square[] getBoard() {
		return board;
	}
	public void setBoard(Square[] board) {
		this.board = board;
	}
	public Piece[] getPiecesStatus() {
		return piecesStatus;
	}
	public void setPiecesStatus(Piece[] piecesStatus) {
		this.piecesStatus = piecesStatus;
	}
	
}
